package org.simpleProxy;

import java.io.File;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.xml.DOMConfigurator;

public class LoggingConfigurator {

	static Logger logger = Logger.getLogger(LoggingConfigurator.class.getName());
	
	public LoggingConfigurator(File configurationFile) {
		
		//Read the provided configuration file and set the logger options, if the file exists
		if(configurationFile.canRead()){
			DOMConfigurator.configure(configurationFile.getAbsolutePath());
			//PropertyConfigurator.configure(configurationFile.getAbsolutePath());
			logger.info("Read configuration file" + configurationFile.getAbsolutePath());
		} 
		//manually set all the default options
		else {
			
			//All console appends
			ConsoleAppender consoleLogger = new ConsoleAppender();
			PatternLayout layout = new PatternLayout();
			layout.setConversionPattern(PatternLayout.TTCC_CONVERSION_PATTERN);
			consoleLogger.setLayout(layout);
			
			//Log backend stuff
			//consoleLogger.setName("org.apache.ftpserver");
			consoleLogger.setThreshold(Level.INFO);
			consoleLogger.activateOptions();
			Logger rootLogger = Logger.getRootLogger();
			//rootLogger.addAppender(consoleLogger);
			
			//Log main Class
			consoleLogger.setName("org.simpleProxy.JavaFTPProxy");
			consoleLogger.activateOptions();
			rootLogger.addAppender(consoleLogger);
			
			//Log Lucene Class
			consoleLogger.setName("org.simpleProxy.LuceneFTPlet");
			consoleLogger.activateOptions();
			rootLogger.addAppender(consoleLogger);
			
			//Log basic configurator
			consoleLogger.setName("org.simpleProxy.BasicConfigurator");
			consoleLogger.activateOptions();
			rootLogger.addAppender(consoleLogger);
			
			//Log GenericLuceneDocumentHandler
			consoleLogger.setName("org.luceneEngine.GenericLuceneDocumentHandler");
			consoleLogger.activateOptions();
			rootLogger.addAppender(consoleLogger);
			
			//Log LuceneTxtHandler
			consoleLogger.setName("org.luceneEngine.LuceneTxtHandler");
			consoleLogger.activateOptions();
			rootLogger.addAppender(consoleLogger);
			
			
			//Log this class
			consoleLogger.setName("org.simpleProxy.LoggingConfigurator");
			consoleLogger.activateOptions();
			rootLogger.addAppender(consoleLogger);
			logger.info("Manually set logging options");
		}
	}
}
